﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace POS
{
    public partial class ClockINOUTScreen : DevExpress.XtraEditors.XtraUserControl
    {
        int ServerID;// = StaticProperties.ServerID;

        public ClockINOUTScreen()
        {
            InitializeComponent();
            listView1.BackColor = this.BackColor;
            listView1.ForeColor = this.ForeColor;

            LoadEmployeeInfo();
        }

        public void LoadEmployeeInfo()
        {
            ServerID = StaticProperties.ServerID;
            DateTime ClockIN;
            DateTime ClockOut;
            DateTime Date;
            TimeSpan DayTotal;

            foreach (DataRow Employee in DataTableClass.EmployeeLogin.Rows)
            {
                if (Employee["EmployeeID"].ToString() == ServerID.ToString())
                {
                    lblEmployeeName.Text = Employee["EmployeeFullName"].ToString().Trim();
                    lblCurrentDateTime.Text = DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToLongTimeString();

                    foreach (DataRow item in DataTableClass.EmployeeTimeSheet.Rows)
                    {
                        if(item["EmployeeID"].ToString() == ServerID.ToString())
                        {
                            Date = DateTime.Parse(item["Date"].ToString().Trim());
                            ClockIN = DateTime.Parse(item["ClockIN"].ToString().Trim());
                            ClockOut = DateTime.Parse(item["ClockOUT"].ToString().Trim());

                            DayTotal = ClockOut - ClockIN;

                            ListViewItem item1 = new ListViewItem(Date.ToShortDateString());
                            item1.SubItems.Add(ClockIN.ToLongTimeString());
                            item1.SubItems.Add(ClockOut.ToLongTimeString());
                            item1.SubItems.Add(DayTotal.TotalHours.ToString());

                            listView1.Items.Add(item1);
                        }
                    }

                    //foreach (DataRow item in DataTableClass.EmployeeSchedule.Rows)
                    //{
                        //if (item["EmployeeID"].ToString() == ServerID.ToString())
                        //{
                                DateTime Today = DateTime.Today;
                                int offset = Today.DayOfWeek - DayOfWeek.Sunday;

                                DateTime lastSunday = Today.AddDays(-offset);

                                FillSchedule(lastSunday);
                        //}
                    //}
                }
            }
        }

        private void FillSchedule(DateTime WeekDate)
        {
            lblWeekOfDate.Text = WeekDate.ToLongDateString();

            foreach (DataRow item in DataTableClass.EmployeeSchedule.Rows)
            {
                if (item["EmployeeID"].ToString() == ServerID.ToString())
                {
                    lblMondayAM.Text = item["MondayAM"].ToString().Trim();
                    lblMondayPM.Text = item["MondayPM"].ToString().Trim();
                    lblTuesdayAM.Text = item["TuesdayAM"].ToString().Trim();
                    lblTuesdayPM.Text = item["TuesdayPM"].ToString().Trim();
                    lblWednesdayAM.Text = item["WednesdayAM"].ToString().Trim();
                    lblWednesdayPM.Text = item["WednesdayPM"].ToString().Trim();
                    lblThursdayAM.Text = item["ThursdayAM"].ToString().Trim();
                    lblThursdayPM.Text = item["ThursdayPM"].ToString().Trim();
                    lblFridayAM.Text = item["FridayAM"].ToString().Trim();
                    lblFridayPM.Text = item["FridayPM"].ToString().Trim();
                    lblSaturdayAM.Text = item["SaturdayAM"].ToString().Trim();
                    lblSaturdayPM.Text = item["SaturdayPM"].ToString().Trim();
                    lblSundayAM.Text = item["SundayAM"].ToString().Trim();
                    lblSundayPM.Text = item["SundayPM"].ToString().Trim();
                }
                else
                {
                    lblMondayAM.Text = "";
                    lblMondayPM.Text = "";
                    lblTuesdayAM.Text = "";
                    lblTuesdayPM.Text = "";
                    lblWednesdayAM.Text = "";
                    lblWednesdayPM.Text = "";
                    lblThursdayAM.Text = "";
                    lblThursdayPM.Text = "";
                    lblFridayAM.Text = "";
                    lblFridayPM.Text = "";
                    lblSaturdayAM.Text = "";
                    lblSaturdayPM.Text = "";
                    lblSundayAM.Text = "";
                    lblSundayPM.Text = "";
                }
            }
        }

        private void btnClockIN_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            PrintingClass.PrintEmployeeSchedulePage();
        }

        private void btnClockOUT_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }
    }
}
